package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.AppManager;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.InsuranceBean;
import com.oscar.ehome.bean.pagebean.InuranceMessageBean;
import com.oscar.ehome.bean.pagebean.WorkMessageBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.dateview.DateDialog;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.view.InsuranceView;

import net.tycmc.bulb.androidstandard.utils.JsonUtils;

import org.apache.commons.collections.MapUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/8/31
 * 保险档安页下一步填写
 */

public class InsurWritesActivity extends BasesActivity<InsuranceView, PagePresenter> implements InsuranceView {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 年交保费
     */
    @BindView(R.id.year_cost_edtext)
    EditText yearCostEdtext;
    /**
     * 缴费期间
     */
    @BindView(R.id.cost_time_edtext)
    EditText costTimeEdtext;
    /**
     * 保障期间
     */
    @BindView(R.id.baozhang_time_edtext)
    EditText baozhangTimeEdtext;
    /**
     * 保额
     */
    @BindView(R.id.baoe_edtext)
    EditText baoeEdtext;
    /**
     * 保险责任
     */
    @BindView(R.id.insura_duty_edtext)
    EditText insuraDutyEdtext;
    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * 产品名称
     */
    @BindView(R.id.chanpin_name_edtext)
    EditText chanpinNameEdtext;
    /**
     * 承保时间
     */
    @BindView(R.id.chengb_time_edtext)
    TextView chengbTimeEdtext;
    /**
     * 承保时间
     */
    @BindView(R.id.chengb_time_layout)
    RelativeLayout chengbTimeLayout;
    /**
     * 保存
     */
    @BindView(R.id.next_buton)
    Button nextButon;

    String dates = "";
    String token = "";
    String message = "";
    String addm = "";
    String id = "";
    Map<String, String> map = new HashMap<String, String>();
    PagePresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insurawrites);
        ButterKnife.bind(this);
        setColor();
        initView();
    }

    @Override
    protected PagePresenter createPresenter() {
        presenter = new PagePresenter(this);
        return presenter;
    }

    /**
     * 改变颜色
     */
    public void setColor() {
        TintManager.setTint(nextButon, Color.parseColor(SystemConfigFactory.getInstance(InsurWritesActivity.this).getSystemConfig().getColor()));
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(InsurWritesActivity.this).getSystemConfig().getColor()));
    }

    /**
     * 初始化
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText(getString(R.string.insurance_text));
        message = getIntent().getStringExtra("mesage");
        addm = getIntent().getStringExtra("addm");
        map = JsonUtils.fromJsonToHashMap(message);
        token = SystemConfigFactory.getInstance(InsurWritesActivity.this).getSystemConfig().getToken();

        if ("2".equals(addm)) {
            setMessage();
        }
    }

    /**
     * 填充信息
     */
    public void setMessage() {
        String message = SystemConfigFactory.getInstance(InsurWritesActivity.this).getSystemConfig().getInaurance();
        InuranceMessageBean bean = new Gson().fromJson(message, InuranceMessageBean.class);
        if (bean != null) {
            id = bean.getId();
            chanpinNameEdtext.setText(bean.getProductName());
            chengbTimeEdtext.setText(bean.getUnderwritingTimeStr());
            yearCostEdtext.setText(bean.getAnnualPremium());
            costTimeEdtext.setText(bean.getPaymentPeriod());
            baozhangTimeEdtext.setText(bean.getGuaranteePeriod());
            baoeEdtext.setText(bean.getInsuredAmount());
            insuraDutyEdtext.setText(bean.getInsuranceDuty());
        }
    }


    @OnClick({R.id.title_layout_left, R.id.next_buton, R.id.chengb_time_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left://返回
                finish();
                break;
            case R.id.next_buton://保存
                if ("".equals(chanpinNameEdtext.getText().toString()) && "".equals(chengbTimeEdtext.getText().toString()) &&
                        "".equals(yearCostEdtext.getText().toString()) && "".equals(costTimeEdtext.getText().toString()) &&
                        "".equals(baozhangTimeEdtext.getText().toString()) && "".equals(baoeEdtext.getText().toString()) &&
                        "".equals(insuraDutyEdtext.getText().toString())) {
                    ToastUtil.show(this, "请填写信息");
                } else {
                    presenter.upload_insurance(id, MapUtils.getString(map, "companyName"), MapUtils.getString(map, "policyNumber"),
                            MapUtils.getString(map, "policyStatus"), MapUtils.getString(map, "insuredPerson"), token, MapUtils.getString(map, "insured")
                            , MapUtils.getString(map, "beneficiaries"), MapUtils.getString(map, "transferBank"), MapUtils.getString(map, "transferAccountNumber"),
                            chanpinNameEdtext.getText().toString(), chengbTimeEdtext.getText().toString(), yearCostEdtext.getText().toString(),
                            costTimeEdtext.getText().toString(), baozhangTimeEdtext.getText().toString(), baoeEdtext.getText().toString(),
                            insuraDutyEdtext.getText().toString(), MapUtils.getString(map, "insuredPersonMobile")
                    );
                    showLoading();
                }
                break;
            case R.id.chengb_time_layout://承保时间
                getDate();
                break;
        }
    }

    /**
     * 日期选择
     */
    public void getDate() {
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        final int[] date = {0, 0, 0};
        DateDialog birthDiolog = new DateDialog(this,
                new DateDialog.PriorityListener() {
                    @Override
                    public void refreshPriorityUI(String year, String month, String date) {
                        // TODO Auto-generated method stub
                        dates = (year + "-" + month + "-" + date);
                        chengbTimeEdtext.setText(dates);
                    }
                }, date[0], date[1], date[2], width, height);
        Window window = birthDiolog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        birthDiolog.setCancelable(true);
        birthDiolog.show();
    }

    /**
     * 上传保险档案
     *
     * @param result
     */
    @Override
    public void upload_mesage(String result) {
        dismissLoading();
        ToastUtil.show(this, "上传成功");
        AppManager.getAppManager().finishActivity(InsurawriteActivity.class);
        if ("2".equals(addm)) {
            AppManager.getAppManager().finishActivity(InsuranceMessageActivity.class);
        }
        finish();
    }

    @Override
    public void upload_worklog(String result) {

    }

    @Override
    public void getInsuranceMessage(InuranceMessageBean result) {

    }

    @Override
    public void getWorkMessage(WorkMessageBean result) {

    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
